package com.lijiajian.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijiajian.base.BaseService;
import com.lijiajian.dao.CusDevPlanMapper;
import com.lijiajian.dao.SaleChanceMapper;
import com.lijiajian.pojo.CusDevPlan;
import com.lijiajian.pojo.SaleChance;
import com.lijiajian.query.CusDevPlanQuery;
import com.lijiajian.utils.AssertUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CusDevPlanService extends BaseService<CusDevPlan,Integer> {
    @Resource
    private CusDevPlanMapper cusDevPlanMapper;
    @Resource
    private SaleChanceMapper saleChanceMapper;
    /**
     * 多条件查询客户开发计划
     * @param cusDevPlanQuery
     * @return
     */
    public Map<String,Object> queryCusDevPlanByParams(CusDevPlanQuery cusDevPlanQuery){
        Map<String,Object> map=new HashMap<>();
        //开启分页
        PageHelper.startPage(cusDevPlanQuery.getPage(),cusDevPlanQuery.getLimit());
        PageInfo<CusDevPlan> pageInfo=new PageInfo<>(cusDevPlanMapper.selectByParams(cusDevPlanQuery));
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());

        return map;
    }

    /**
     * 添加客户开发计划数据
     * @param cusDevPlan
     */
    public void addCusDevPlan(CusDevPlan cusDevPlan){
        //参数校验
        checkCusDevPlanParams(cusDevPlan);
        //设置默认值
        cusDevPlan.setIsValid(1);
        cusDevPlan.setCreateDate(new Date());
        cusDevPlan.setUpdateDate(new Date());

        AssertUtil.isTrue(cusDevPlanMapper.insertSelective(cusDevPlan)<1,"添加失败");


    }

    /**
     * 更新客户开发计划
     * @param cusDevPlan
     */
    public void updateCusDevPlan(CusDevPlan cusDevPlan){
        //参数校验
        AssertUtil.isTrue(cusDevPlan.getId()==null || cusDevPlanMapper.selectByPrimaryKey(cusDevPlan.getId())==null,"记录不存在");
        checkCusDevPlanParams(cusDevPlan);
        //设置默认值

        cusDevPlan.setUpdateDate(new Date());

        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan)<1,"更新失败");
    }

    /**
     * 删除客户开发计划
     * @param id
     */
    public void deleteCusDevPlan(Integer id){
        //参数校验
        AssertUtil.isTrue(id==null,"数据异常");
        CusDevPlan cusDevPlan = cusDevPlanMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(cusDevPlan==null,"记录不存在");
        cusDevPlan.setUpdateDate(new Date());
        cusDevPlan.setIsValid(0);

        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan)<1,"删除失败");
    }


    private void checkCusDevPlanParams(CusDevPlan cusDevPlan) {
        //营销机会Id非空，数据存在
        Integer id = cusDevPlan.getSaleChanceId();
        AssertUtil.isTrue(null==id ||saleChanceMapper.selectByPrimaryKey(id)==null,"数据异常，请重试");
        //计划项内容非空
        AssertUtil.isTrue(null==cusDevPlan.getPlanItem(),"计划项内容为空");
        //计划时间非空
        AssertUtil.isTrue(null==cusDevPlan.getPlanDate(),"计划时间为空");
    }


}
